#include "wait_processbar.h"

#include <QTimer>
#include <QPainterPath>
#include <QPainter>

WaitProcessBar::WaitProcessBar(QWidget *pParent)
{
    this->resize(100, 100);
    timer = new QTimer();

    // 定时旋转坐标系
    connect(timer, &QTimer::timeout,
            this, &WaitProcessBar::updaterRotation);


    this->setModal(true);//设置为模态，该窗口不关闭，父窗口不能操作

    //隐藏标题栏
    //this->setWindowFlags(Qt::FramelessWindowHint);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

    //背景透明
    setAttribute(Qt::WA_TranslucentBackground, true);
}

WaitProcessBar::~WaitProcessBar()
{
    if (timer)
    {
        delete timer;
        timer = nullptr;
    }
}

void WaitProcessBar::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);

    QPainter painter(this);

    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width / 2, height / 2);
    painter.scale(side / 200.0, side / 200.0);

    QConicalGradient gra(QPoint(0, 0), 0);
    gra.setColorAt(0, QColor("#3BB6FE"));
    gra.setColorAt(1, QColor("#FFFFFF"));
    QBrush brush(gra);

    int radis = 40;
    int sider = 10;
    QRect rect(-radis, -radis, radis * 2, radis * 2);
    QPainterPath path;
    path.arcTo(rect, 0, 270);

    QPainterPath subPath;
    subPath.addEllipse(rect.adjusted(sider, sider, -sider, -sider));
    //这行注释掉就是圆形，加上去就是环形
    path = path - subPath;

    painter.setBrush(brush);
    painter.setPen(Qt::NoPen);
    painter.rotate(rotation);
    painter.drawPath(path);



}

void WaitProcessBar::updaterRotation()
{
    rotation++;
    if (rotation == 360)
    {
        rotation = 0;
    }
    update();
}

void WaitProcessBar::startTimer()
{
    timer->start(10);
    this->exec();
}

void WaitProcessBar::stopTimer()
{
    timer->stop();
    this->close();
}

